package org.springblade.common.config;

import lombok.AllArgsConstructor;
import org.springblade.core.oss.rule.OssRule;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;

/**
 * @Description Oss规则，重写BladeOssRule，增加自定义文件名称
 * @Author ysq
 * @Date 2023/5/26
 **/
@AllArgsConstructor
public class BladeOssRuleExt implements OssRule {

	/**
	 * 租户模式
	 */
	private final Boolean tenantMode;

	/**
	 * 自定义文件命标识
	 */
	public static final String DIV_FILE_NAME = "自定义-";

	@Override
	public String bucketName(String bucketName) {
		String prefix = (tenantMode) ? AuthUtil.getTenantId().concat(StringPool.DASH) : StringPool.EMPTY;
		return prefix + bucketName;
	}

	@Override
	public String fileName(String originalFilename) {
		if (originalFilename.contains(DIV_FILE_NAME)) {
			return AuthUtil.getTenantId() + "/upload" + StringPool.SLASH + DateUtil.today() + StringPool.SLASH + originalFilename.replace(DIV_FILE_NAME, "");
		}
		return AuthUtil.getTenantId() + "/upload" + StringPool.SLASH + DateUtil.today() + StringPool.SLASH + StringUtil.randomUUID() + StringPool.DOT + FileUtil.getFileExtension(originalFilename);

	}
}
